<?php
declare (strict_types = 1);

namespace app\home\model;
use think\Model;


/**
 * @mixin \think\Model
 */
class User extends Model
{
    protected $name="user";

/*    protected $autoWriteTimestamp="int";

    protected $createTime='create_time';

    protected $updateTime='update_time';*/

    public function updateBalace($id,$type='inc',$field,$amount){
         $this->where(['id' => $id])->$type($field, $amount)->update();
         return true;
    }

    public function updateUserLevel($id,$amount){
        $levelModel  = new Level();
        $levelInfo = $levelModel->where('consumption_amount','<=',$amount)
            ->order('consumption_amount','desc')->find();
        $UserLevelModel = new UserLevel();
        $userLevel = $UserLevelModel->where(['level_id' =>$levelInfo['id'],'uid'=> $id ])->find();
        if (empty($userLevel)){
            $data = [
                'level_id' => $levelInfo['id'],
                'uid' => $id,
                'create_time' => time(),
            ];;
            $UserLevelModel-> insert($data);
            $this->where(['id' => $id])->update(['level_id'=> $levelInfo['id'],'level_name' => $levelInfo['name']]);

        }
        return true;

    }

}



